package com.ujs.code.shuzu;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.IntStream;

/**
 * 1.删除排序数组中的重复项
 *
 * @author zhangshihao
 * @create 2023-09-07 15:42
 */
public class RemoveDuplicates {
    public static void main(String[] args) {
        int[] nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
        int i = removeDuplicates2(nums);
    }

    public static int removeDuplicates(int[] nums) {
        IntStream stream = Arrays.stream(nums);
        ArrayList<Integer> arrayList = new ArrayList<>();
        stream.peek(item -> {
            if (!arrayList.contains(item)) {
                arrayList.add(item);
            }
        }).toArray();
        for (int i = 0; i < arrayList.size(); i++) {
            nums[i] = arrayList.get(i);
        }
        return arrayList.size();
    }

    /**
     * 指针，左指针和右指针
     *
     * @param nums
     * @return
     */
    public static int removeDuplicates2(int[] nums) {
        int left = 0;
        for (int right = 1; right < nums.length; right++) {
            // 当两个数字不相同的时候，给left+1，并在left+1的位置赋予不同的那个值
            if (nums[left] != nums[right]) {
                nums[++left] = nums[right];
            }
        }
        // System.out.println(left);
        return ++left;
    }
}
